﻿//Código desenvolvido por João Ribeiro
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Configuration;

namespace GestaoContas
{
    public partial class VerSala : Form
    {
        private int _salaId;
        private string _connectionStringJardimBD;
        private Sala _sala;

        public VerSala()
        {
            InitializeComponent();
        }

        public VerSala(string nomeSala)
        {
            InitializeComponent();
            _sala = new Sala();
            _sala.PreencheObjectoSala(nomeSala);

            textBoxNomeSala.Text = _sala.NomeSala;
            textBoxObservacoes.Text = _sala.Observacoes;
            _salaId = _sala.SalaId;
            _connectionStringJardimBD = ConfigurationManager.ConnectionStrings["ConnectionStringJardimBD"].ConnectionString;

            PreencheGridViewEducandos();
            textBoxNumeroEducandos.Text = GetContagemDeEducandos().ToString();
        }

        /// <summary>
        /// Preenche a gridview dos educandos.
        /// </summary>
        private void PreencheGridViewEducandos()
        {
            string query = @"SELECT Nome, DataNascimento as ""Data de nascimento"", Observacoes as ""Observação"" FROM Crianca WHERE Sala_SalaId = " + _salaId;

            try
            {
                SqlCeConnection conn = new SqlCeConnection(_connectionStringJardimBD);
                SqlCeCommand cmd = new SqlCeCommand(query, conn);

                SqlCeDataAdapter da = new SqlCeDataAdapter();
                DataSet ds = new DataSet();

                da.SelectCommand = cmd;

                da.Fill(ds);
                dataGridViewEducandos.DataSource = ds.Tables[0];
                dataGridViewEducandos.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private int GetContagemDeEducandos()
        {
            string query = @"SELECT COUNT (*) FROM Crianca WHERE Sala_SalaId = " + _salaId;
            int contagem = 0;

            SqlCeConnection conn = new SqlCeConnection(_connectionStringJardimBD);
            conn.Open();
            try
            {
                using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                {
                    using (SqlCeDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        reader.Read();
                        contagem = int.Parse(reader[0].ToString());
                        reader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return contagem;
        }

        private void buttonVoltar_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
